﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using mozhi.smarterp.Business.Payments.Repositories;
using mozhi.smarterp.Business.Purchases;
using mozhi.smarterp.Business.Purchases.Repositories;
using mozhi.smarterp.Business.RemoteRepositories.Payments;
using mozhi.smarterp.Business.RemoteRepositories.Purchases;
using Mozhi.Abp.Domain.AuthorCenter;
using Mozhi.Abp.Domain.AuthorCenter.Repositories;
using Mozhi.Abp.Domain.AuthorCenter.Repository;
using Mozhi.Abp.Domain.BasicData;
using Mozhi.Abp.Domain.BasicData.Repositories;
using Mozhi.Abp.Domain.Repository.AuthorCenter.RemoteRepositories;
using Mozhi.Abp.Domain.Repository.BasicData;
using Mozhi.Abp.Domain.Repository.BasicData.RemoteRepositories;
using Volo.Abp.EntityFrameworkCore;
using Volo.Abp.EntityFrameworkCore.DependencyInjection;
using Volo.Abp.Modularity;

namespace mozhi.smarterp.Business.EntityFrameworkCore;

[DependsOn(
    typeof(BusinessDomainModule),
    typeof(AbpEntityFrameworkCoreModule)
)]
public class BusinessEntityFrameworkCoreModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        context.Services.AddAbpDbContext<BusinessDbContext>(options =>
        {
            /* Add custom repositories here. Example:
             * options.AddRepository<Question, EfCoreQuestionRepository>();
             */
        });

        Configure<AbpEntityOptions>(options =>
        {
            options.Entity<PurchaseRequisition>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.Items);
            });

            options.Entity<PurchaseOrder>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.Items).Include(o => o.AdvancePaymentItems);
            });

            options.Entity<PurchaseProductPrice>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.Items);
            });

            options.Entity<PurchaseReceipt>(op =>
            {
                op.DefaultWithDetailsFunc = query => query.Include(o => o.Items);
            });
        });

        context.Services.AddScoped(typeof(IProductRepository), typeof(ProductRepository));
        context.Services.AddScoped(typeof(ISupplierRepository), typeof(SupplierRepository));
        context.Services.AddScoped(typeof(IUnitRepository), typeof(UnitRepository));
        context.Services.AddScoped(typeof(IEmployeeRepository), typeof(EmployeeRepository));
        context.Services.AddScoped(typeof(IProductAuxiliaryUnitRepository), typeof(ProductAuxiliaryUnitRepository));
        context.Services.AddScoped(typeof(IWarehouseRepository), typeof(WarehouseRepository));
        context.Services.AddScoped(typeof(IDepartmentRepository), typeof(DepartmentRepository));
        context.Services.AddScoped(typeof(INumberGeneratorRepository), typeof(NumberGeneratorRepository));
        context.Services.AddScoped(typeof(ICurrencyRepository), typeof(CurrencyRepository));
        context.Services.AddScoped(typeof(ITaxRateRepository), typeof(TaxRateRepository));
        context.Services.AddScoped(typeof(IOrganizationRepository) , typeof(OrganizationRepository));
        context.Services.AddScoped(typeof(IPurchaseReceiptPayableRepository), typeof(PurchaseReceiptPayableRepository));
        context.Services.AddScoped(typeof(IPaymentRequisitonRepository), typeof(PaymentRequisitionRepository));
        context.Services.AddScoped(typeof(IApiMetadataRepository), typeof(ApiMetadataRepository));
    }
}